package br.com.findimoveis.application;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.findimoveis.domain.Anunciante;
import br.com.findimoveis.domain.Anuncio;
import br.com.findimoveis.domain.Imovel;

import com.google.appengine.api.datastore.AsyncDatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Key;


public class ImovelCadatroController extends BaseController {
 
	private static final long serialVersionUID = 7146856787981566722L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
		  throws ServletException, IOException {
		

		Imovel imovel = new Imovel().createNewImovel(
				new Anunciante(request.getParameter("nomeanunciante"), request
						.getParameter("telefoneanunciante"), request
						.getParameter("emailanunciante")),
				new Anuncio(request.getParameter("tituloAnuncio")), Double
						.valueOf(request.getParameter(VALOR)), Double
						.valueOf(request.getParameter(AREA_UTI)), Integer
						.valueOf(request.getParameter(DORMITORIO)),request.getParameter("descricao"), null);



		Key key = null;
		AsyncDatastoreService asyncDatastoreService = DatastoreServiceFactory
				.getAsyncDatastoreService();
		
		try {
			key = asyncDatastoreService.put(imovel.getEntity()).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	
//		Query query = new Query(key.getKind());
//		PreparedQuery preparedQuery = asyncDatastoreService.prepare(query);
//		Entity asSingleEntity = preparedQuery.asSingleEntity();

		//asyncDatastoreService.get(key).isCancelled();
		
		request.setAttribute("imovel", imovel);
     	forward(request, response, "/show.jsp");
	}
}
